﻿using ZK.MyControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZK.BLL.Config;
using Utils.Common;
using System.Configuration;

namespace ZK.Client
{
    public partial class FrmRegisterInfo : Form
    {
        public FrmRegisterInfo(string msg)
        {
            InitializeComponent();
            this.Resize += FrmMessageBox_Resize;
            _msg = msg;
        }

        string _msg;

        private void FrmMessageBox_Load(object sender, EventArgs e)
        {
            textBox1.Text = _msg;
        }

        private void FrmMessageBox_Resize(object sender, EventArgs e)
        {
            SetWindowRegion.SetWindowSadius(20, this);
        }

        private void btnActive_Click(object sender, EventArgs e)
        {
            if (SoftAuthorize.Instance.IsRegistered(textBox2.Text.Trim()))
            {
                UpdateAppConfig("RegisterCode", textBox2.Text.Trim());
                MessageBox.Show("激活成功,请重新运行程序！");
                this.Close();
            }
        }


        ///<summary>  
        ///在＊.exe.config文件中appSettings配置节增加一对键、值对  
        ///</summary>  
        ///<param name="newKey"></param>  
        ///<param name="newValue"></param>  
        private   void UpdateAppConfig(string newKey, string newValue)
        {
            bool isModified = false;
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == newKey)
                {
                    isModified = true;
                }
            }

            // Open App.Config of executable  
            Configuration config =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // You need to remove the old settings object before you can replace it  
            if (isModified)
            {
                config.AppSettings.Settings.Remove(newKey);
            }
            // Add an Application Setting.  
            config.AppSettings.Settings.Add(newKey, newValue);
            // Save the changes in App.config file.  
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.  
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
